package finance

import (
	emodel "engine/model"
	"fmt"
	"github.com/xuri/excelize/v2"
	"lib/directory"
	"lib/log"
	"strconv"
	"time"
)

func ExportFinancial(list []*emodel.FinancialList, isSendDaily bool, filePath string) (string, error) {
	f := excelize.NewFile()
	colWidths := map[string]float64{
		"B": 12,
		"C": 18,
		"D": 20,
		"E": 20,
		"F": 20,
		"K": 20,
		"N": 20,
		"P": 20,
		"Q": 20,
	}
	for widthKey, widthVal := range colWidths {
		if err := f.SetColWidth("Sheet1", widthKey, widthKey, widthVal); err != nil {
			log.Errorf("SetColWidth() error:%v", err)
			return "", err
		}
	}
	title := [][]string{
		[]string{"A1", "订单类型"},
		[]string{"B1", "单号"},
		[]string{"C1", "订单状态"},
		[]string{"D1", "第三方单号"},
		[]string{"E1", "客户"},
		[]string{"F1", "客户结算价"},
		[]string{"G1", "支付方式"},
		[]string{"H1", "手续费"},
		[]string{"I1", "支付总额"},
		[]string{"J1", "供应商"},
		[]string{"K1", "供应商结算价/状态"},
		[]string{"L1", "利润"},
		[]string{"M1", "销售"},
		[]string{"N1", "票号"},
		[]string{"O1", "张数"},
		[]string{"P1", "出票时间"},
		[]string{"Q1", "机票创建时间"},
	}
	for _, titleArr := range title {
		if err := f.SetCellValue("Sheet1", titleArr[0], titleArr[1]); err != nil {
			log.Errorf("SetCellValue() error:%v", err)
			return "", err
		}
	}
	index := 2
	for _, financialItem := range list {
		if err := setCells(f, financialItem, index); err != nil {
			panic(err)
		}
		index++
	}
	if err := f.MergeCell("Sheet1", "A"+strconv.Itoa(index), "E"+strconv.Itoa(index)); err != nil {
		log.Errorf("MergeCell() error:%v", err)
		return "", err
	}
	if err := f.SetCellValue("Sheet1", "A"+strconv.Itoa(index), "总计:"); err != nil {
		log.Errorf("SetCellValue() error:%v", err)
		return "", err
	}
	if err := f.SetCellFormula("Sheet1", "F"+strconv.Itoa(index), "SUM(F2:F"+strconv.Itoa(index-1)+")"); err != nil {
		log.Errorf("SetCellFormula() error:%v", err)
		return "", err
	}
	if err := f.SetCellFormula("Sheet1", "I"+strconv.Itoa(index), "SUM(I2:I"+strconv.Itoa(index-1)+")"); err != nil {
		log.Errorf("SetCellFormula() error:%v", err)
		return "", err
	}
	if err := f.SetCellFormula("Sheet1", "L"+strconv.Itoa(index), "SUM(L2:L"+strconv.Itoa(index-1)+")"); err != nil {
		log.Errorf("SetCellFormula() error:%v", err)
		return "", err
	}
	if err := f.SetCellFormula("Sheet1", "O"+strconv.Itoa(index), "SUM(O2:O"+strconv.Itoa(index-1)+")"); err != nil {
		log.Errorf("SetCellFormula() error:%v", err)
		return "", err
	}

	filePathTemp := fmt.Sprintf("resources/financial/%s.xlsx", time.Now().Format("2006-01-02")) //"resources/financial/" + time.Now().Format("2006-01-02") + ".xlsx"
	if isSendDaily {
		isOk, err := directory.PathExists("resources/financial/sendDaily", true)
		if err != nil || !isOk {
			log.Errorf("PathExists: %v %v", isOk, err)
			return "", err
		}
		filePathTemp = fmt.Sprintf("resources/financial/sendDaily/%s.xlsx", time.Now().AddDate(0, 0, -1).Format("2006-01-02")) //"resources/financial/sendDaily/" + time.Now().AddDate(0, 0, -1).Format("2006-01-02") + ".xlsx"
	}

	if err := f.SaveAs(filePathTemp); err != nil {
		log.Errorf("SaveAs() error:%v", err)
		return "", err
	}

	return filePathTemp, nil
}
